package test.threads;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ThreadsActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
	public double progress=0.0;
	public ProgressBar bar;
	public boolean isRunning;
	public static Context cxt;
	public Thread background;
	public Handler glove = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			progress+=.1;
			bar.setProgress((int)(progress));
		}
	};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bar = (ProgressBar) findViewById(R.id.progressBar1);
        cxt=this;
        ((Button) findViewById(R.id.button1)).setOnClickListener(this);
    }
    @Override
    public void onStart() {
    	super.onStart();
    	bar.setProgress(0);
    	background = new Thread(new Runnable(){

			public void run() {
				try {
					if (!isRunning) {
						
					}
					for (int i=0; i>=0; i++) {
						if (isRunning) {
								
							Thread.sleep(5);
							glove.sendMessage(glove.obtainMessage());
						}
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
    		
    	});
    	isRunning=true;
    	background.start();
    }
    @Override
    public void onStop() {
    	super.onStop();
    	isRunning=false;
    }
    public static void toast(String txt) {
    	Toast.makeText(cxt, txt, Toast.LENGTH_LONG).show();
    }
	public void onClick(View arg0) {
		isRunning=!isRunning;
	}
    
    
}